﻿BaiduOCR(){
	; http://ai.baidu.com/docs#/Begin/top
	apiKey := "7U2OgTyTuH5NG2ZAxHZfbfQR"
	secretKey := "yFklkou5XySgF1WkxBeKmePphkOESFhO"
	; 获取Access Token
	; http://ai.baidu.com/docs#/Auth/top
	whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	whr.Open("GET", "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" apiKey "&client_secret=" secretKey)
	whr.Send()
	ret := Json_Load(whr.ResponseText)
	if !ret.access_token
		throw ret.error "`n" ret.error_description

	; 通用文字识别
	whr.Open("POST", "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" ret.access_token)
	whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
	; img:=%imgPath%
	b64Data := Base64Enc_FromFile("test.png")
	body := "image=" UriEncode(b64Data) "&language_type=CHN_ENG"
	whr.Send(body)

	Txtmp:=GetWinHttpText(whr)
	OutputVar:=GetWinHttpText(whr)

	; 测试保存数据到 txt 文件
	; FileDelete, %A_ScriptDir%\text.txt
	; Sleep, 100
	; FileAppend , %Txtmp%, %A_ScriptDir%\text.txt, UTF-8
	; FileRead, OutputVar, %A_ScriptDir%\text.txt
	
	RegExMatch(OutputVar,"\[.*\]",getTx)
	get01 := RegExReplace(getTx,"\[|\]","")
	get02 := RegExReplace(get01,"words","")
	get03 := RegExReplace(get02,"""","")
	get04 := RegExReplace(get03,"{: ","")
	get05 := RegExReplace(get04,"},","`n")
	keyword := RegExReplace(get05,"}","")
	return keyword
return  
}

GetWinHttpText(objWinHttp, encoding := "UTF-8") {
	ado := ComObjCreate("adodb.stream")
	ado.Type     := 1 ; adTypeBinary = 1
	ado.Mode     := 3 ; adModeReadWrite = 3
	ado.Open()
	ado.Write(objWinHttp.ResponseBody)
	ado.Position := 0
	ado.Type     := 2 ; adTypeText = 2
	ado.Charset  := encoding
	return ado.ReadText(), ado.Close()
}